﻿----------------------------------------
-- Looat auto confirm
----------------------------------------
local LootConfirmer = CreateFrame("Frame")

LootConfirmer:RegisterEvent("CONFIRM_DISENCHANT_ROLL");
LootConfirmer:RegisterEvent("CONFIRM_LOOT_ROLL");
LootConfirmer:SetScript('OnEvent',
	function(self, event, ...)
		local arg1, arg2 = ...;
		ConfirmLootRoll(arg1, arg2);
	end
)
StaticPopupDialogs["CONFIRM_LOOT_ROLL"] = nil

----------------------------------------
-- autoinvite
----------------------------------------
local AinvEnabled = true
local AinvKeywords = {
    ["invite"] = true,
    ["inv"] = true,
    ["инв"] = true,
    ["инвайт"] = true
}
local AutoInvite = CreateFrame("frame")
AutoInvite:RegisterEvent("CHAT_MSG_WHISPER")
AutoInvite:SetScript("OnEvent",
    function(self, event, arg1, arg2)
        if AinvKeywords[arg1:lower()]
            and AinvEnabled
            and not GetLFGQueueStats()
            and ((not UnitExists("party1") or IsPartyLeader("player") or IsRaidOfficer("player") or IsRaidLeader("player"))) then
                InviteUnit(arg2)
        end
end)

----------------------------------------
-- alignment grid
----------------------------------------
SLASH_EA1 = "/align"

local AlignFrame

SlashCmdList["EA"] = function()
	if AlignFrame then
		AlignFrame:Hide()
		AlignFrame = nil
	else
		local f = CreateFrame('Frame', nil, UIParent)
        AlignFrame = f
		f:SetAllPoints(UIParent)
		local w = GetScreenWidth() / 64
		local h = GetScreenHeight() / 36
		for i = 0, 64 do
			local t = f:CreateTexture(nil, 'BACKGROUND')
			if i == 32 then
				t:SetTexture(1, 0, 0, 0.5)
			else
				t:SetTexture(0, 0, 0, 0.5)
			end
			t:SetPoint('TOPLEFT', f, 'TOPLEFT', i * w - 1, 0)
			t:SetPoint('BOTTOMRIGHT', f, 'BOTTOMLEFT', i * w + 1, 0)
		end
		for i = 0, 36 do
			local t = f:CreateTexture(nil, 'BACKGROUND')
			if i == 18 then
				t:SetTexture(1, 0, 0, 0.5)
			else
				t:SetTexture(0, 0, 0, 0.5)
			end
			t:SetPoint('TOPLEFT', f, 'TOPLEFT', 0, -i * h + 1)
			t:SetPoint('BOTTOMRIGHT', f, 'TOPRIGHT', 0, -i * h - 1)
		end
	end
end

----------------------------------------
-- chat clear
----------------------------------------
SLASH_CLS1 = "/cls"
SLAHS_CLS2 = "/сды"
SLASH_CLS3 = "/клс"

SlashCmdList["CLS"] = function ()
    SELECTED_CHAT_FRAME:Clear()
end

----------------------------------------
-- aura applied by
----------------------------------------
local function SetUnitAura_Hook(self,unit,index,filter)
	local _, _, _, _, _, _, _, casterUnit = UnitAura(unit, index, filter)
	if UnitExists(casterUnit) then
		self:AddLine(format("<Applied by %s>", UnitName(casterUnit) or UNKNOWN), 0.2, 0.6, 1)
		self:Show()
	end
end
hooksecurefunc(GameTooltip, "SetUnitAura", SetUnitAura_Hook)

----------------------------------------
-- health warning
----------------------------------------
local HealthWatcher = CreateFrame('Frame')
HealthWatcher.lastUpdateTime = 0
HealthWatcher.UPDATE_INTERVAL = 0.1
HealthWatcher.lastSoundTime = 0
HealthWatcher.flashStartTime = 0
HealthWatcher.visible = false
function HealthWatcher:CreateFlasher()
	local flasher = CreateFrame('Frame')
	flasher:SetToplevel(true)
	flasher:SetFrameStrata('FULLSCREEN_DIALOG')
	flasher:SetAllPoints(UIParent)
	flasher:EnableMouse(false)
	flasher.texture = flasher:CreateTexture(nil, 'BACKGROUND')
	flasher.texture:SetTexture('Interface\\FullScreenTextures\\LowHealth')
	flasher.texture:SetAllPoints(UIParent)
	flasher.texture:SetBlendMode('ADD')
	flasher:SetAlpha(0)
	flasher:Hide()
	return flasher
end
function HealthWatcher:Init()
	self.flasher = self:CreateFlasher()
	self:SetScript("OnUpdate", self.update)
end
function HealthWatcher:update(elapsed)
	local ctime = GetTime()
	local elapsed = ctime - self.lastUpdateTime
	if elapsed < HealthWatcher.UPDATE_INTERVAL then
		return
	end
	self.lastUpdateTime = ctime
	local health = UnitHealth('player')
	if not UnitIsDeadOrGhost('player') and (health / UnitHealthMax('player') < 0.3) and not UnitOnTaxi('player') then
		if (ctime - self.lastSoundTime) > 1.5 then
			PlaySoundFile('Interface\\Addons\\ps\\media\\hbeat.mp3', 'Master')
			self.lastSoundTime = ctime
		end
		if not self.visible then
			self.flasher:SetAlpha(0)
			self.flasher:Show()
			self.visible = true
			self.flashStartTime = ctime
		else
			local alpha = (ctime - self.flashStartTime) % 0.7
			if alpha > 0.35 then
				alpha = 0.7 - alpha
			end
			self.flasher:SetAlpha(alpha * 5)
		end
	elseif self.visible then
		self.flasher:SetAlpha(0)
		self.flasher:Hide()
		self.visible = false
	end
end
HealthWatcher:Init()

----------------------------------------
-- pull cooldown
----------------------------------------
local pull_cooldown_remain = 0
local pull_cooldown_next_tick = 0
local PullCooldownFrame = CreateFrame('Frame')
function PullCooldownFrame:report(msg)
	local channel = 'SAY'

	if GetNumRaidMembers() > 0 then
		if IsRaidLeader() or IsRaidOfficer() then
			channel = 'RAID_WARNING'
		else
			channel = 'RAID'
		end
	end
	SendChatMessage(msg, channel)
end

function PullCooldownFrame:update(elapsed)
	if GetTime() >= pull_cooldown_next_tick then
		pull_cooldown_next_tick = GetTime() + 1
		if pull_cooldown_remain == 0 then
			self:SetScript('OnUpdate', nil)
			self:report('{череп}{череп}{череп}{череп} ПУЛЛ! {череп}{череп}{череп}{череп}')
		else
			local msg = tostring(pull_cooldown_remain)
			if pull_cooldown_remain == 1 then
				msg = '{круг}{круг} '..msg..' {круг}{круг}'
			end
			self:report(msg)
		end
		pull_cooldown_remain = pull_cooldown_remain - 1
	end
end

SLASH_PC1 = '/pc'
SlashCmdList['PC'] = function(seconds)
	if seconds == '' then
		pull_cooldown_remain = 5
	else
		pull_cooldown_remain = tonumber(seconds)
	end
	if pull_cooldown_remain < 1 then
		PullCooldownFrame:SetScript('OnUpdate', nil)
	else
		pull_cooldown_next_tick = GetTime()
		PullCooldownFrame:SetScript('OnUpdate', PullCooldownFrame.update)
	end
end
